<template>
	<navigator :url="`/pages/common/vito/index?id=${localData.id}`">
		<view class="layout">
			<view class="img-box">
				<image :src="localData.coverImage" class="img" mode="aspectFill"></image>
				<view class="status">
					{{ getStatusText(localData.rankingStatus) }}
				</view>
			</view>
			<view class="text-box">
				<view class="text-layout">
					<view class="title">
						<text>{{ localData.topic }}</text>
					</view>
					<view class="icon" @click.stop="toggleFavorite">
						<uni-icons type="star-filled" size="30"
							:color="localData.is_favorite === 1 ? '#D81E06' : '#BFBFBF'">
						</uni-icons>
					</view>
				</view>
				<view class="time-box">
					<view class="time">
						<view class="">
							开始时间
						</view>
						<view class="time-test">
							{{ formatTime(localData.startTime) }}
						</view>
					</view>
					<view class="time">
						<view class="">
							结束时间
						</view>
						<view class="time-test">
							{{ formatTime(localData.endTime) }}
						</view>
					</view>
				</view>
			</view>
		</view>
	</navigator>
</template>

<script setup>
	import {
		ref,
		watch
	} from 'vue';
	import {
		formatTime
	} from '@/utils/time';
	import {
		AppCreateFavoriteApi,
		AppDeleteFavoriteApi
	} from '@/api/index';
	import {
		useUserStore
	} from '@/store';

	// 接收 props
	const props = defineProps({
		data: {
			type: Object,
			required: true
		}
	});

	// 创建本地响应式对象（避免直接修改 props）
	const localData = ref({
		...props.data
	});

	// 监听 props 变化，确保 localData 同步更新
	watch(() => props.data, (newVal) => {
		localData.value = {
			...newVal
		};
	}, {
		deep: true
	});

	const userStore = useUserStore();

	const toggleFavorite = async () => {
		const userId = userStore.id;
		const rankingId = localData.value.id;

		try {
			if (localData.value.is_favorite === 1) {
				await AppDeleteFavoriteApi(userId, rankingId);
				localData.value.is_favorite = 0; // 取消收藏
			} else {
				await AppCreateFavoriteApi(userId, rankingId);
				localData.value.is_favorite = 1; // 收藏成功
			}
		} catch (error) {
			console.error('收藏操作失败', error);
		}
	};

	const getStatusText = (status) => {
		switch (status) {
			case 0:
				return "未开始";
			case 1:
				return "正在进行";
			case 2:
				return "已经结束";
			default:
				return "";
		}
	};
</script>

<style lang="scss" scoped>
	.layout {
		display: flex;
		margin-top: 20rpx;
		height: 200rpx;
	}

	.img-box {
		width: 200rpx;
		height: 200rpx;
		position: relative;
	}

	.status {
		position: absolute;
		top: 10rpx;
		right: 10rpx;
		background-color: rgba(0, 0, 0, 0.6);
		color: #fff;
		padding: 5rpx 10rpx;
		font-size: 20rpx;
		border-radius: 10rpx;
	}

	.img {
		width: 100%;
		height: 100%;
		object-fit: cover;
	}

	.title {
		height: 120rpx;
		margin-left: 10rpx;
		width: 400rpx;
		font-size: 25rpx;
	}

	.time-box {
		font-size: 20rpx;
		margin-top: 20rpx;
		margin-left: 15rpx;
	}

	.time {
		display: flex;
	}

	.time-test {
		margin-left: 15rpx;
	}

	.text-layout {
		display: flex;
	}

	.icon {
		margin-left: 20rpx;
	}
</style>